package com.teamfractal.fracdustry.common.block.impl.generators;

import com.teamfractal.fracdustry.common.block.init.FDAbstractHDBlock;
import com.teamfractal.fracdustry.common.blockentity.Generators.FDVawtBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ObjectHolder;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

import static net.minecraft.ChatFormatting.AQUA;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class FDVawtBlock extends FDAbstractHDBlock implements EntityBlock {

    public static final String NAME = "fracdustry:vawt";

    @ObjectHolder(NAME)
    public static FDVawtBlock BLOCK;

    public FDVawtBlock(){
        super(Properties.of(Material.METAL)
                .sound(SoundType.METAL)
                .lightLevel(state -> 0)
                .strength(2.0f).noOcclusion());
    }


    @Nonnull
    @Override
    public RenderShape getRenderShape(@Nonnull BlockState state)
    {
        return RenderShape.ENTITYBLOCK_ANIMATED;
    }


    @Override
    public BlockEntity newBlockEntity(@Nonnull BlockPos pos, @Nonnull BlockState state) {
        //return new FDVawtBlockEntity(pos, state);
        return FDVawtBlockEntity.BLOCK_ENTITY_TYPE.create(pos, state);
    }

    //Connect BlockEntity Ticking
    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, @Nonnull BlockState state, @Nonnull BlockEntityType<T> type) {
        if (level.isClientSide()) {
            return null;
        } else {
            return (level1, pos, state1, tile) -> {
                if (tile instanceof FDVawtBlockEntity generator) {
                    generator.tickServer(state1);
                }
            };
        }
    }


    //BlockItem's tooltip
    @Override
    public void appendHoverText(@Nonnull ItemStack stack, @Nullable BlockGetter reader, List<Component> list, @Nonnull TooltipFlag flags) {
        list.add(new TranslatableComponent("tooltips.fracdustry.vawt").withStyle(AQUA));
    }

    @Nonnull
    @Override
    @SuppressWarnings("deprecation")
    public VoxelShape getShape(@Nonnull BlockState state, @Nonnull BlockGetter world, @Nonnull BlockPos pos, @Nonnull CollisionContext context)
    {
        return Shapes.or(Block.box(7, 0, 7, 9, 28, 9));
    }
}
